﻿#include "fsadddialog.h"
#include "ui_fsadddialog.h"
#include <QMessageBox>
#include <QComboBox>
#include <QSqlQuery>
FSaddDialog::FSaddDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FSaddDialog)
{
    ui->setupUi(this);
    ui->tbRow->horizontalHeader()->setResizeMode(QHeaderView::Stretch); //自适应列宽
    ui->dtEdtSignTime->setDate(QDate::currentDate());
    ui->dtEdtSmtTime->setDate(QDate::currentDate());
    ui->frame->resize(this->geometry().width(), ui->frame->height());
    ui->status->setText("添加信息状态：");
    connect(ui->btnOk,SIGNAL(clicked(bool)),parent,SLOT(showpage()));
}

FSaddDialog::~FSaddDialog()
{
    delete ui;}

void FSaddDialog::on_btnAddRow_clicked()
{
    int rowCount = ui->tbRow->rowCount();
    if(rowCount > 2){
        ui->status->setText("添加信息状态：档案人信息不能超过三条！");
    }else{
        ui->tbRow->insertRow(rowCount);
        QComboBox *comBox = new QComboBox();
        comBox->addItem("居民身份证");
        comBox->addItem("军官证");
        comBox->addItem("士官证");
        comBox->addItem("义务兵证");
        comBox->addItem("港澳台居民居住证");
        comBox->addItem("外籍人士在我国永久居住证");
        comBox->addItem("护照");
        comBox->addItem("其他");
        ui->tbRow->setCellWidget(rowCount,1,comBox);//单元格嵌入combox

        QDateEdit * dtEdit = new QDateEdit();
        dtEdit->setDate(QDate::currentDate());
        dtEdit->setCalendarPopup(true);
        dtEdit->setDisplayFormat("yyyy-MM-dd");
        dtEdit->setStyleSheet("QDateEdit{background:rgb(255, 255, 255)}");//设置背景使之不透明
        ui->tbRow->setCellWidget(rowCount,4,dtEdit);
    }
}

void FSaddDialog::on_btnDelRow_clicked()
{
    int rowIndex = ui->tbRow->currentRow();
    if(ui->tbRow->rowCount() == 0){
        ui->status->setText("添加信息状态：请先添加行！");
    }else if(rowIndex < 0){
        ui->status->setText("添加信息状态：请先选中行！");
    }else{
        ui->tbRow->removeRow(rowIndex);
    }
}


void FSaddDialog::on_btnCancel_clicked()
{
    this->close();
}

void FSaddDialog::on_btnOk_clicked()
{
    QStringList listRow;//存放row的sql插入语句

    strSbmTime = ui->dtEdtSmtTime->date().toString("yyyy-MM-dd");//报送时间
    strSbtUnit = ui->edtSmtUnit->text();//报送单位
    strSignTime = ui->dtEdtSignTime->date().toString("yyyy-MM-dd");//签发时间
    strSignPeople = ui->edtSignPeople->text();//签发人
    strDuringPeople = ui->edtDuringPeople->text();//经办人
    strTel = ui->edtTel->text();//联系电话

    const char *s = strTel.toUtf8().data();
    while(*s && *s>='0' && *s<='9')s++;//bool(*s)返回false为数字

    if(strSbtUnit.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入报送单位！");
    }else if(strSbtUnit.size() > 120){//120
        ui->status->setText("添加信息状态：报送单位信息输入有误！");
    }else if(strSignPeople.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入签发人！");
    }else if(strSignPeople.size() > 50){//50
        ui->status->setText("添加信息状态：签发人信息输入有误！");
    }else if(strDuringPeople.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入经办人！");
    }else if(strDuringPeople.size() > 50){//50
        ui->status->setText("添加信息状态：经办人信息输入有误！");
    }else if(strTel.trimmed().isEmpty()){
        ui->status->setText("添加信息状态：请输入联系电话！");
    }else if(bool(*s)){
        ui->status->setText("添加信息状态：联系电话输入有误！");
    }else{
        //如果行表上方信息输入无误，开始判断表格信息是否输入有误
        int rowCount = ui->tbRow->rowCount();
        if(rowCount == 0){//如果无行信息
            ui->status->setText("添加信息状态：请输入档案相关信息！");
            }else{//如果有行信息
            int flagRow = 0;//设置符号判定行信息是否输入正确
            for(int i=0;i<rowCount;i++){//开始逐行判断信息是否输入错误
                for(int j=0;j<6;j++){
                    if(j == 0){
                        if(ui->tbRow->item(i,j) == NULL || ui->tbRow->item(i,j)->text().trimmed().isEmpty()){
                            ui->status->setText(QString("添加信息状态：请输入第%1行的姓名！").arg(i+1));
                            break;
                        }else if(ui->tbRow->item(i,j)->text().size() > 50){
                            ui->status->setText(QString("添加信息状态：第%1行的姓名信息输入有误！").arg(i+1));
                            break;
                        }
                    }else if(j == 2){
                        if(ui->tbRow->item(i,j) == NULL || ui->tbRow->item(i,j)->text().trimmed().isEmpty()){
                            ui->status->setText(QString("添加信息状态：请输入第%1行的证件号！").arg(i+1));
                            break;
                        }else if(ui->tbRow->item(i,j)->text().size() > 18){
                            ui->status->setText(QString("添加信息状态：第%1行的证件号信息输入有误！").arg(i+1));
                            break;
                        }
                    }else if(j == 3){
                        if(ui->tbRow->item(i,j) == NULL || ui->tbRow->item(i,j)->text().trimmed().isEmpty()){
                            ui->status->setText(QString("添加信息状态：请输入第%1行的职务！").arg(i+1));
                            break;
                        }else if(ui->tbRow->item(i,j)->text().size() > 80){
                            ui->status->setText(QString("添加信息状态：第%1行的职务信息输入有误！").arg(i+1));
                            break;
                        }
                    }else if(j == 5){
                        const char *s;
                        if(ui->tbRow->item(i,j) != NULL){
                            s = ui->tbRow->item(i,j)->text().toUtf8().data();
                            while(*s && *s>='0' && *s<='9')s++;//bool(*s)返回false为数字
                        }
                        if(ui->tbRow->item(i,j) == NULL || ui->tbRow->item(i,j)->text().trimmed().isEmpty()){
                            ui->status->setText(QString("添加信息状态：请输入第%1行的卷数！").arg(i+1));
                            break;
                        }else if(bool(*s)){//如果是数字
                            ui->status->setText(QString("添加信息状态：第%1行的卷数信息输入有误！").arg(i+1));
                            break;
                        }else{//如果行信息输入无误，开始拼接插入字符串
                            QString strName = ui->tbRow->item(i,0)->text();//姓名
                            QComboBox *cmb = (QComboBox *)ui->tbRow->cellWidget(i,1);
                            QString strIDType;
                            if(cmb->currentIndex() != 7){
                                strIDType = QString::number(cmb->currentIndex()+1);
                            }else{
                                strIDType = QString::number(cmb->currentIndex()+2);
                            }
                            QString strID = ui->tbRow->item(i,2)->text();
                            QString strDuty = ui->tbRow->item(i,3)->text();
                            QDateEdit *dtDate = (QDateEdit *)ui->tbRow->cellWidget(i,4);
                            QString strDate = dtDate->date().toString("yyyy-MM-dd");
                            int num = ui->tbRow->item(i,5)->text().toInt();
                            QString strInsert =QString("'%1','%2','%3','%4','%5','%6'); ")
                                    .arg(strName).arg(strIDType).arg(strID).arg(strDuty).arg(strDate).arg(num);
                            listRow.append(strInsert);
                            flagRow++;
                        }
                    }
                }
            }
            if(rowCount == flagRow){
                //再判断下方两个txt是否输入规范
                strQuestion = ui->txtQuestion->toPlainText();
                strSuggestion = ui->txtSuggestion->toPlainText();
                if(strQuestion.size() > 800){
                    ui->status->setText("添加信息状态：档案遗留问题或需要说明的情况超出规定字数！");
                }else if(strSuggestion.size() > 150){
                    ui->status->setText("添加信息状态：签发意见超出规定字数！");
                }else{
                    QString strInsert=QString("insert into FileSbmtPage values (null,'%1','%2','%3','%4','%5','%6','%7','%8'); ")
                    .arg(strSbmTime).arg(strSbtUnit).arg(strSignPeople).arg(strSignTime)
                    .arg(strDuringPeople).arg(strTel).arg(strQuestion).arg(strSuggestion);
                    QSqlQuery query;
                    if(query.exec(strInsert)){
                        //表单信息已添加，开始添加行
                        QSqlQuery queryR;
                        QString strSelectR=QString("SELECT * FROM FileSbmtPage;");
                        if(queryR.exec(strSelectR)){
                            if(queryR.last()){//获取最新添加的表单id
                                int flaga = 0;//判断插入语句执行几次的标志
                                QString pageID = queryR.value(0).toString();
                                QString strRowInsert1 = QString("insert into FileSbmtRow values (null,%1,").arg(pageID);
                                for(int i=0;i<listRow.size();i++){
                                    QString strRowInsert2 = strRowInsert1 + listRow.at(i);//将插入语句拼接完整
                                    QSqlQuery query2;
                                    if(query2.exec(strRowInsert2)){
                                        flaga++;
                                    }
                                }
                                if(listRow.size() == flaga){
                                    ui->status->setText("添加信息状态：表单信息登记成功！");
                                    this->close();
                                }else{
                                    //如果添加行失败则将最近一次添加的单信息删除
                                    QString strDel = QString("DELETE FROM FileSbmtPage WHERE `FileSbmtID`='%1';").arg(pageID);
                                    QSqlQuery queryS;
                                    queryS.exec(strDel);
                                    QString strDel2 = QString("DELETE FROM FileSbmtRow WHERE `FileSbmtID`='%1';").arg(pageID);
                                    QSqlQuery queryS2;
                                    queryS2.exec(strDel2);
                                    ui->status->setText("添加信息状态：表单信息登记失败！");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
